package com.study.service.impl;

import com.study.dao.BookPointDao;
import com.study.pojo.BookPoint;
import com.study.service.BookPointService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class BookPointServiceImpl implements BookPointService {
    @Resource
    private BookPointDao bookPointDao;

    public BookPointServiceImpl() {
    }

    public BookPoint queryById(Integer id) {
        return this.bookPointDao.queryById(id);
    }

    public BookPoint queryByCon(Integer userId, Integer bookId) {
        return this.bookPointDao.queryByCon(userId, bookId);
    }

    public List<BookPoint> queryAllByLimit(int offset, int limit) {
        return this.bookPointDao.queryAllByLimit(offset, limit);
    }

    public void insertOrUpdate(BookPoint bookPoint) {
        BookPoint point = this.bookPointDao.queryByCon(bookPoint.getUserId(), bookPoint.getBookId());
        if (point == null) {
            this.bookPointDao.insert(bookPoint);
        } else {
            bookPoint.setId(point.getId());
            this.bookPointDao.update(bookPoint);
        }

    }

    public BookPoint update(BookPoint bookPoint) {
        this.bookPointDao.update(bookPoint);
        return this.queryById(bookPoint.getId());
    }

    public boolean deleteById(Integer id) {
        return this.bookPointDao.deleteById(id) > 0;
    }

    public List<BookPoint> queryAllPoints(Integer bookId) {
        return this.bookPointDao.queryAllPoints(bookId);
    }

}
